package com.returnsoft.resource.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.resource.dto.CountryDto;
import com.returnsoft.resource.eao.CountryEao;
import com.returnsoft.resource.entity.Country;
import com.returnsoft.resource.exception.ConversionExcepcion;
import com.returnsoft.resource.exception.EaoExcepcion;
import com.returnsoft.resource.exception.RecruitmentException;
import com.returnsoft.resource.util.Conversion;

/**
 * Session Bean implementation class CountryBean
 */
@Stateless
@LocalBean
public class CountryBean implements CountryInterface {

	@EJB
	private CountryEao countryEao;
	@EJB
	private Conversion conv;

	/**
	 * Default constructor.
	 */
	public CountryBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<CountryDto> findAll() throws RecruitmentException {
		try {
			List<Country> countries = countryEao.findAll();
			List<CountryDto> countriesDto = new ArrayList<CountryDto>();
			for (Country country : countries) {
				CountryDto countryDto = conv.fromCountry(country);
				countriesDto.add(countryDto);
			}
			return countriesDto;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

}
